import axios from 'axios'
export async function loadArcGIS ({ entityCollection, layerData}) {
    let { data } = await axios.get(layerData.url, {});
    if(data.geometryType === "esriGeometryPoint"){
        data.features.forEach(item => drawPoint(entityCollection, item));
    }else if(data.geometryType === "esriGeometryPolygon"){
        data.features.forEach(item => drawPolygon(entityCollection, item));
    }else if(data.geometryType === "esriGeometryPolyline"){
        data.features.forEach(item => drawPolyline(entityCollection, item));
    }
}

function drawPoint (entityCollection, jsonData) {
    const { geometry } = jsonData
    const p1 = Cesium.Cartesian3.fromDegrees(geometry.x, geometry.y, 0)
    entityCollection.entities.add({
        position: p1, // 组成的点
        point: {
          pixelSize: 7,
          outlineWidth: 2,
          color:  new mars3d.material.CircleWaveMaterialProperty({
            color: Cesium.Color.fromCssColorString("#4b0082").withAlpha(0.5)
          }),
          outlineColor:  Cesium.Color.fromCssColorString("#0000ff").withAlpha(0.5),
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
          distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
        },
    })
}

function drawPolygon (entityCollection, jsonData) {
    const points = jsonData.geometry.rings
    points.forEach(item => {
        entityCollection.entities.add({
            show: true,
            polygon: {
                hierarchy: getPositions(item),
                // material: Cesium.Color.fromCssColorString("#000").withAlpha(1),
                material: new mars3d.material.PolyGradientMaterialProperty({
                    color: Cesium.Color.fromCssColorString("#00ffff").withAlpha(0.5),
                    diffusePower: 1.6
                }),
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
            }
        });
    });
}

function drawPolyline(entityCollection, jsonData){
    const points = jsonData.geometry.paths
    points.forEach(item => {
        entityCollection.entities.add({
            show: true,
            polyline: {
                positions: getPositions(item), //[经度，纬度，经度，纬度...]
                width: 5,
                material: Cesium.Color.fromCssColorString("#00ffff"),
                distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
                // clampToGround: true
                //arcType: Cesium.ArcType.GEODESIC
            },
        }); 
    })
}

function getPositions(points) {
    const positions = []
    points.forEach(item => {
        positions.push(item[0])
        positions.push(item[1])
    })
    return Cesium.Cartesian3.fromDegreesArray(positions)
}